package zhh.tangbao.system.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 2023/2/28
 * @author 粥灰灰
 * @version 1.0
 * mybatis-plus扩展插件配置类
 */

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    class SqlInjector extends DefaultSqlInjector {
        @Override
        public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
            List<AbstractMethod> methodList = super.getMethodList(mapperClass);
            // 给BaseMapper添加一个批量添加的插件，并且方法为insertBatchSomeColumn
            // 这样每次继承BaseMapper并自行添加insertBatchSomeColumn方法后即可实现批量插入的功能
            methodList.add(new InsertBatchSomeColumn());
            // 添加InsertBatchSomeColumn方法
            return methodList;
        }
    }

    @Bean
    public SqlInjector SqlInjectorConfig() {
        return new SqlInjector();
    }
}
